package com.testin.dao.impl;

import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import javax.sql.DataSource;

import com.testin.dao.UserTestingDao;
import com.testin.model.Quest;
import com.testin.model.Require;
import com.testin.model.User;
import com.testin.utils.MysqlLoader;


/**
 * 用户测试信息dao
 * @author liuqiang
 *
 */
public class UserTestingDaoImpl implements UserTestingDao{
	private DataSource dataSource;

	/**
	 * 保存测试信息
	 */
	public int saveQuest(List<Quest> questList) throws Exception{
		if(questList!=null&&questList.size()>0){
			MysqlLoader loader = new MysqlLoader();
			int countNum=0;
			for (int i = 0; i < questList.size(); i++) {
				Quest quest =questList.get(i);
				String sql = "insert into quest_data(test_id,type,data_info,date_time) values (" +
				quest.getTestId()+" ," +
				getSplitString(quest.getType())+" ," +
				getSplitString(quest.getDataInfo())+" ," +
				getSplitString(formatDate(quest.getDateTime(),"yyyy-MM-dd HH:mm:ss"))
				+" )";
				loader.saveData(sql);
				countNum++;
			}
			
			return countNum;
		}else{
			return 0;
		}
		
		//return (Integer)write.insert("UserTestingDao.saveQuest", quest);
	}
	
	/**
	 * 保存用户信息
	 */
	public int saveUser(User user) throws Exception{
		if(user!=null){
			MysqlLoader loader = new MysqlLoader();
			String sql = "insert into user_info (name,phone,im_code,mail,cmp_name,cmp_address,cmp_url,date_time) values" +
					" (" +
					getSplitString(user.getName())+" ," +
					getSplitString(user.getPhone())+" ," +
					getSplitString(user.getImCode())+" ," +
					getSplitString(user.getMail())+" ," +
					getSplitString(user.getCmpName())+" ," +
					getSplitString(user.getCmpAddress())+" ," +
					getSplitString(user.getCmpUrl())+ " ," +
					getSplitString(formatDate(user.getDateTime(),"yyyy-MM-dd HH:mm:ss"))+" )";
			return loader.saveData(sql);
		}else{
			return 0;
		}
		
	}
	
	/**
	 * 日期格式转换
	 * @param date
	 * @param pattern
	 * @return
	 */
	public String formatDate(Date date, String pattern){
        if(date == null){
            date = new Date();
        }
        if(pattern==null){
        	pattern="yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
	
	
	public String getSplitString(String param){
		if(null!=param){
			if (param.contains("'")){
				param = param.replaceAll("'", "''");
			}
			return "'" + param + "'";
		}else{
			return "''";
		}
	}
	
	
	public int saveRequire(Require require) throws Exception{
		if(require!=null){
			MysqlLoader loader = new MysqlLoader();
			String sql = "insert into require_info (app_type,app_url,app_desc,app_test_scene,p_quantity,p_sex,p_age_min,p_age_max,p_revenue,p_useterm," +
					"p_require_other,platform_type,user_Id,date_time ) values " +
					" (" +
					getSplitString(require.getAppType())+" ," +
					getSplitString(require.getAppUrl())+" ," +
					getSplitString(require.getAppDesc())+" ," +
					getSplitString(require.getAppTestScene())+" ," +
					require.getpQuantity()+" ," +
					getSplitString(require.getpSex())+" ," +
					require.getpAgeMin()+ " ," +
					require.getpAgeMax()+ " ," +
					getSplitString(require.getpRevenue())+ " ," +
					getSplitString(require.getpUseterm())+ " ," +
					getSplitString(require.getpRequireOther())+ " ," +
					getSplitString(require.getPlatformType())+ " ," +
					require.getUserId()+ " ," +
					getSplitString(formatDate(require.getDateTime(),"yyyy-MM-dd HH:mm:ss"))
					+" )";
			return loader.saveData(sql);
		}else{
			return 0;
		}
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}	
	
}
